package com.moody.init;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.dao.CodeLibDAO;
import com.moody.hibernate.domain.CodeLib;

public class DataDictionary {
	private static final Logger log = LoggerFactory
			.getLogger(DataDictionary.class);
	private static DataDictionary instance = null;
	static WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	static ServletContext context = web.getServletContext();  
	static WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	private static CodeLibDAO codeLibDAO = CodeLibDAO.getFromApplicationContext(ctx);
	private static Map<String, Map<String, CodeLib>> map = new LinkedHashMap<String, Map<String, CodeLib>>();

	private DataDictionary() {

	}

	public static DataDictionary getInstance() {
		if (instance == null) {
			instance = new DataDictionary();
			instance.getDataDictionary();
		}
		return instance;
	}

	/*
	 * 写入数据字典
	 */
	@SuppressWarnings("unchecked")
	public  Map<String, Map<String, CodeLib>> getDataDictionary() {	

		if (map.size() > 0) {
			//log.info("数据字典Map已生成 ");
		} else {
			List<CodeLib> list = codeLibDAO.findAll();
			for (CodeLib codeLib : list) {
				if (!map.containsKey(codeLib.getId().getCodeNo())) {
					if(codeLib.getId().getCodeNo().equals("ReportScope")){
						Map<String, CodeLib> rSubMap = new LinkedHashMap<String, CodeLib>();
						rSubMap.put(codeLib
								.getItemName(), codeLib);
						map.put("ReverseReportScope", rSubMap);
					}
					Map<String, CodeLib> subMap = new LinkedHashMap<String, CodeLib>();
					subMap.put(codeLib.getId().getItemNo(), codeLib);
					map.put(codeLib.getId().getCodeNo(), subMap);
				} else {
					if(codeLib.getId().getCodeNo().equals("ReportScope")){
						map.get("ReverseReportScope").put(codeLib
								.getItemName(), codeLib);
					}
					map.get(codeLib.getId().getCodeNo()).put(
							codeLib.getId().getItemNo(), codeLib);
					
				}
			}
			
		}
		return map;
	}

	/*
	 * 通过codeNo和ItemNo查找ItemName
	 */
	public String getNameByCodeAndId(String codeNo, String itemNo) {
		if (codeNo == null || codeNo.equals("") || itemNo == null
				|| itemNo.equals("")) {
			log.error("codeNo or itemNo not exist! codeNo: "+codeNo+" itemNo: "+itemNo);
			return null;
		} else {
			return DataDictionary.getInstance().getDataDictionary().get(codeNo).get(itemNo).getItemName();
		}
	}
	
	public String getIdNoByCodeAndName(String codeNo, String itemName){
		if (codeNo == null || codeNo.equals("") || itemName == null
				|| itemName.equals("")) {
			log.error("codeNo or itemNo not exist! codeNo: "+codeNo+" itemNo: "+itemName);
		} else {
			Set<String> keySet = map.get(codeNo).keySet();
			for(String ks : keySet){
				if(itemName.equals(map.get(codeNo).get(ks).getItemName()))
					return ks;
			}
		}
		return null;
	}
}
